jQuery(function($){
	//x轴坐标格式处理
	function getTime(dataLen){
	    var timesTamp = '';
		var curTime=new Date();
		var ts=curTime.getTime(); //改为时间戳
		//ts -= 10 * 60 * 1000;
		//alert(ts);
		for (var i = 0; i < dataLen; i++) {
			ts -= 1 * 60 * 1000;  //相差dataLen分钟 每次减一分钟的时间戳
			//alert(ts);
			var utc= new Date(ts);
			var curHour = utc.getHours(); 
			var curMinute = utc.getMinutes();
			if(curHour < 10){
				curHour = "0" + curHour;
			}
			if(curMinute < 10){
				curMinute = "0" + curMinute;
			}
			curTime = curHour + ':' + curMinute;
			var timesTampTemp = "\""+  curTime  +"\""+ ",   ";
			timesTamp = timesTampTemp + timesTamp  ;
		}
		return timesTamp;
		//alert(timesTamp);
   }
	
	
	function getCassandraGraph(){

		//cpu /detection/cpu/cassandra/usage  cpu_cassandra
		var cpu_len = 0;//得到一条线里面有几个数据数目 用于计算时间点
		var cpu_len = 0;//得到一条线里面有几个数据数目 用于计算时间点
		   var cpu_json = {};
		   var cpu_chart = {
		      type: 'spline'      
		   }; 
		   var cpu_title = {
		      text: '<b>CPU利用率<b>'   
		   };
		   var cpu_subtitle = {
		      text: '每一分钟更新一次'
		   };
		   var cpu_xAxis = {
		       categories: ['1', '2', '3', '4', '5', '6',
		          '7', '8', '9', '10']
		   };
		   var cpu_yAxis = {
		      title: {
		         text: ' 利用率 '
		      },
		      plotLines: [{
		         value: 0,
		         width: 1,
		         color: '#808080'
		      }]
		   };
			
			$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/cpu/cassandra/usage", 
		        success: function (data) {
		        	//修改json格式，改为画图可以用的
		        	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(cpu_len < data[i].data.length){
		                	 cpu_len = data[i].data.length;
		                 }
	                }
	      		   //提示信息
	      		   var cpu_tooltip = {
	      			  crosshairs: true,
	      		      shared: true,
	      		   };
	      		   //展示方式右对齐
	      		   var cpu_legend = {
	      		      layout: 'vertical',
	      		      align: 'right', //图例位置
	      		      verticalAlign: 'middle',
	      		      borderWidth: 0
	      		   };
		           var  cpu_time = getTime(cpu_len);
		           //alert(thisTime);
		           cpu_xAxis = {
	      		      categories: eval("[" + cpu_time + "]")
	      		   };
		        	
			       //cpu 折线图
				   var cpu_series =  data;
				   cpu_json.tooltip = cpu_tooltip;
				   cpu_json.cpu_legend = cpu_legend;
				   cpu_json.series = cpu_series;
		        }//sucess
		
			});//ajax
			cpu_json.title = cpu_title;
		    cpu_json.subtitle = cpu_subtitle;
		    cpu_json.xAxis = cpu_xAxis;
		    cpu_json.yAxis = cpu_yAxis;
		    $('#cpu_cassandra').highcharts(cpu_json);
		    
		    
		
			//内存折线图数据    memory_cassandra
			var memory_len = 0;
			var memory_len = 0;
			var memory_json = {};
			var memory_chart = {
			     type: 'spline'      
			}; 
		    var memory_title = {
		         text: '<b>内存利用率<b>'   
		    };
		    var memory_subtitle = {
		        text: '每一分钟更新一次'
		    };
		    var memory_xAxis = {
		       categories: ['1', '2', '3', '4', '5', '6',
		          '7', '8', '9', '10']
		    };//默认显示1-10 有数据时显示时间
		    var memory_yAxis = {
		        title: {
		           text: ' 利用率 '
		       },
		       plotLines: [{
		          value: 0,
		          width: 1,
		          color: '#808080'
		       }]
		    };
			$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/memory/cassandra/usage", 
		        success: function (data) {
		        	//修改json格式，改为画图可以用的
		        	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(memory_len < data[i].data.length){
		                	 memory_len = data[i].data.length;
		                 }
	                }
	      		   //提示信息
	      		   var memory_tooltip = {
	      			  crosshairs: true,
	      		      shared: true,
	      		   };
	      		   //展示方式右对齐
	      		   var memory_legend = {
	      		      layout: 'vertical',
	      		      align: 'right', //图例位置
	      		      verticalAlign: 'middle',
	      		      borderWidth: 0
	      		   };
		           var  memory_time = getTime(memory_len);
		           //alert(thisTime);
		           memory_xAxis = {
	      		      categories: eval("[" + memory_time + "]")
	      		   };
			       //cpu 折线图
				   var memory_series =  data;
				   memory_json.tooltip = memory_tooltip;
				   memory_json.memory_legend = memory_legend;
				   memory_json.series = memory_series;
		    	
		        },//sucess
		        error : function(jqXHR, textStatus, errorThrown){
		        	/*alert(jqXHR.responseText);
		            alert(jqXHR.status);
		            alert(jqXHR.readyState);
		            alert(jqXHR.statusText);*/
		        	//alert("故障诊断数据插入失败");
		        }
			});//ajax
			memory_json.title = memory_title;
			memory_json.subtitle = memory_subtitle;
			memory_json.xAxis = memory_xAxis;
			memory_json.yAxis = memory_yAxis;
		    $('#memory_cassandra').highcharts(memory_json);
			
	}
	
	getCassandraGraph();
	setInterval(getCassandraGraph,60000);
	
	
	 
		
	function getCassandraTable(){
		
		var cassandra_metric_row='';
		$.ajax({
			type:"get",
			url: "/detection/db/cassandra",
			cache:false,
			success: function(data){
		       for(var i = 0; i < data.length; i++){    //获取后台传过来的url中的数据
		    	    var time = data[i].time;
		            var cassandra = data[i].cassandra;
		            var hostname = data[i].hostname;
		            var threadCount = data[i].threadCount;
		            var totalStartedThreadCount = data[i].totalStartedThreadCount;
		            var storageLoad = data[i].storageLoad;
		            var storageExceptions = data[i].storageExceptions;
		            var connectedNativeClients = data[i].connectedNativeClients;
		            var connectedThriftClients = data[i].connectedThriftClients;
		            var completedTasks = data[i].completedTasks;
		            var hitcounts = data[i].hitcounts;
		            cassandra_metric_row += '<tr><td>'  + time + '</td><td>' 
		            									+ cassandra + '</td><td>' 
		            									+ hostname +'</td><td>'
		            									/*+ threadCount + '</td><td>' 
		            									+ totalStartedThreadCount + '</td><td>'*/
		            									+ storageLoad +'</td><td>'
		            									+ completedTasks + '</td><td>'
		            									+ hitcounts +'</td></tr>';
		            									/*+ connectedThriftClients +'</td></tr>';*/
		    		
		       };
		       $('#cassandra_metric_body').html(cassandra_metric_row);//将每一行的数据加载到html中
		       $('#cassandra_metric').footable();//对footable进行初始化
		       //alert(cassandra_metric_row);
			},
			error:function(jqXHR, textStatus, errorThrown){
        	    $('#cassandra_metric').footable();//对footable进行初始化
				var cassandra_metric_error = '<tr class="footable-empty"><td colspan="9">No results</td></tr>';
				var tabletTbody = $("#cassandra_metric").find('tbody');
        	    tabletTbody.empty();
        	    tabletTbody.append(cassandra_metric_error);
			}
		
		});		
		
		//pod表信息
		var cassandra_pod_row='';
		var statusValue='';
		var statusStyle='';
		$.ajax({
			type:"get",
			url: "/detection/podInfo/cassandra/list",
			cache:false,
			success: function(data){
				
		       for(var i = 0; i < data.length; i++){    //获取后台传过来的url中的数据
		    	    var podName = data[i].podName;
		            var phase = data[i].phase;
		            if(phase=="Running"){
		            	statusValue=1;
		            }else if(phase=="Down"){
		            	statusValue=0;
		            }
		            
		            switch(statusValue){         //对违反规则的严重程度进行判断,并进行显式转换
		    		case 1:
		    			statusStyle = 'status-circle status-running';
		    			break;
		    		case 0:
		    			statusStyle = 'status-circle status-crash';
		    		 	break;
		    		
		    		default:
		    			statusStyle = 'status-metro status-crash';
		    		};
		    		cassandra_pod_row += '<tr><td>' + podName  + '</td><td><div class="'+statusStyle +'">'+'</div></td></tr>'
		    		
		       };
		       //alert(pod_row);
		       $('#cassandra_pod_body').html(cassandra_pod_row);//将每一行的数据加载到html中
		       $('#cassandra_pod').footable();//对footable进行初始化
			},
			error:function(jqXHR, textStatus, errorThrown){
        	    $('#cassandra_pod').footable();//对footable进行初始化
				var cassandra_pod_error = '<tr class="footable-empty"><td colspan="2">No results</td></tr>';
				var tabletTbody = $("#cassandra_pod").find('tbody');
        	    tabletTbody.empty();
        	    tabletTbody.append(cassandra_pod_error);

			}
		});	
		
		
	}
	
	getCassandraTable();
	setInterval(getCassandraTable,60000);
	
});	
	
	
	/*//增加指标表格按钮
	$('#confirm').bind('click',function(){
		//class="table" border="1"  table table

		$('#tableName').text('Cassandra监控新增指标表');
		//创建表格
		var table=$("<table id=\"newQuota\" border=\"1\" class=\"table\" >");
		   table.appendTo($("#createTable"));
		   for(var i=0;i<1;i++)
		   {
			    var tr=$("<tr></tr>");
			    tr.appendTo(table);
			    for(var j=0;j<arr.length;j++)
				{
				      var td=$("<td>"+arr[j]+"</td>");
				      td.appendTo(tr);
			    }
		   }
		   trend.appendTo(table);
		   //$("#createTable").append("</table>");
		   
		   $.ajax({
				url: "rule/queryRule.action",
				cache:"false",
				success: function(data){
					
			       for(var i = 0; i < data.length; i++){    //获取后台传过来的url中的数据
			    	   //var expression = ;//arr
			    	   
			    	   var tr=$("<tr></tr>");
					    tr.appendTo(table);
					    for(var j=0;j<arr.length;j++)
						{
					    	  var str=arr[j];
						      var td=$("<td>"+data[i].str+"</td>");
						      td.appendTo(tr);
					    }
			    	   
			    	    row += '<tr><td>' +  expression + '</td></tr>';
			    		
			       };
			       $('tbody').html(row);//将每一行的数据加载到html中
			       //console.log(row);
			       $('#newQuota').footable();//对footable进行初始化
			       
				}
			});	
		   $("#createTable").append("</table>");
		   
		   
	});
	

	//var oDiv=$('#Footable');
	//var aCh=oDiv.$('input');
	
	//全选按钮  
	$('#allQuota').bind('click',function(){
		//alert('全选按钮');
		 
        $("input:checkbox[name='quota']").each(function(){  
            //此处如果用attr，会出现第三次失效的情况  
            $(this).prop("checked",true);  
	    });  
				
	});
	
	//反选按钮
	$('#inverseQuota').bind('click',function(){
		 //alert('反选按钮');
		 $("input:checkbox[name='quota']").each(function(){  
			 if($(this).is(':checked')){
				 $(this).removeAttr("checked",false); 
			 }else{
				 $(this).prop("checked",true);
			 }
		 });  
		
	});*/
	/*
	
	*/
